package com.simen.connectandroid.command;

import com.simen.connectandroid.command.utils.CommandCreator;
import com.simen.connectandroid.constants.Constant;
import com.simen.connectandroid.model.Device;
import com.simen.hellobytes.log.Log;
import com.simen.hellobytes.runtime.RuntimeHelper;

/**
 * Created on 2021/10/30 14:44.
 *
 * @author Simen
 */
public class TcpipCommand extends AdbCommand<TcpipCommand> {

    private static final String TAG = TcpipCommand.class.getSimpleName();
    private int port = Constant.PORT;
    private Integer processCode;

    public TcpipCommand(Device device) {
        super(device);
    }

    public TcpipCommand(Device device, int port) {
        super(device);
        this.port = port;
    }

    @Override
    public TcpipCommand exec() {
        System.out.println("tcpip " + port);

        RuntimeHelper runtimeHelper = RuntimeHelper.create(CommandCreator
                .createAdb()
                .appendAdbDevice(device)
                .appendParam("tcpip", "" + port)
                .build());
        runtimeHelper.exec();

        log = runtimeHelper.getOutputLog();
        processCode = runtimeHelper.getProcessCode();

        Log.i(TAG, runtimeHelper.command());
        Log.i(TAG, "ProcessCode: " + runtimeHelper.getProcessCode());
        Log.i(TAG, log);
        return this;
    }

    @Override
    public boolean execResult() {
        return log.contains("restarting in TCP mode port") || processCode == 0;
    }

    public int getPort() {
        return port;
    }
}
